﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EndlessGalaxy
{
    public partial class FacilityBuilder : Form
    {
        Planet here;
        public FacilityBuilder(Planet location)
        {
            InitializeComponent();
            here = location;
            foreach (Cargo stuff in here.surface)
            {
                if (Globals.materials[stuff.name].attributes.Contains("STRUCTURAL"))
                {
                    if (stuff.amount >= 40000.0)
                    {
                        StructureBox.Items.Add(stuff.name);
                    }
                }
                if (Globals.materials[stuff.name].attributes.Contains("CONDUCTOR"))
                {
                    if (stuff.amount >= 400.0)
                    {
                        ConductorBox.Items.Add(stuff.name);
                    }
                }
                if (Globals.materials[stuff.name].attributes.Contains("SEMICONDUCTOR"))
                {
                    if (stuff.amount >= 40.0)
                    {
                        SemiCondBox.Items.Add(stuff.name);
                    }
                }
            }
        }

        private void BuildButton_Click(object sender, EventArgs e)
        {
            Facility construction = new Facility(1, new List<Cargo>() {
                new Cargo(OutBox1.Items[OutBox1.SelectedIndex] as string, 1, Cargo.Phases.solid) }, 'm');
            switch (TypeBox.SelectedIndex)
            {
                case 0://Miner
                    //Handled above, I guess.
                    break;
                case 1://Solar Plant
                    MessageBox.Show("Solar plants are not included yet.");
                    break;
                case 2://Factory
                    MessageBox.Show("Factories are not included yet.");//InBox1 does materials, __Box2's will be objects in and out
                    break;
                case 3://Molecular Foundry
                    Dictionary<string, double> stuff;
                    construction = new Facility(1, new List<Cargo>(), new List<Cargo>());
                    switch (InBox1.SelectedIndex)
                    {
                        case 0:
                            construction.input = new List<Cargo>() { new Cargo(Convert.ToString(OutBox1.Items[OutBox1.SelectedIndex]),
                        1, Cargo.Phases.other) };
                            construction.output = new List<Cargo>();
                            stuff = Globals.materials[Convert.ToString(OutBox1.Items[OutBox1.SelectedIndex])].contents();
                            foreach (string k in stuff.Keys)
                            {
                                construction.output.Add(new Cargo(k, stuff[k], Globals.materials[k].phase));
                            }
                            break;
                        default:
                            construction.output = new List<Cargo>() { new Cargo(Convert.ToString(OutBox1.Items[OutBox1.SelectedIndex]),
                        construction.level, Globals.materials[Convert.ToString(OutBox1.Items[OutBox1.SelectedIndex])].phase) };
                            construction.input = new List<Cargo>();
                            stuff = Globals.materials[Convert.ToString(OutBox1.Items[OutBox1.SelectedIndex])].contents();
                            foreach (string k in stuff.Keys)
                            {
                                construction.input.Add(new Cargo(k, stuff[k], Cargo.Phases.solid));
                            }
                            break;
                    }
                    break;
                case 4://Atomic Foundry
                    MessageBox.Show("Atomic Foundries are not included yet.");
                    break;
                case 5://Shipyard
                    MessageBox.Show("Shipyards are not included yet.");
                    break;
                default:
                    break;
            }
            Cargo.TakeCargo(StructureBox.Items[StructureBox.SelectedIndex] as string, 40000.0, here.surface);
            Cargo.TakeCargo(ConductorBox.Items[ConductorBox.SelectedIndex] as string, 400.0, here.surface);
            Cargo.TakeCargo(SemiCondBox.Items[SemiCondBox.SelectedIndex] as string, 40.0, here.surface);
            here.facilities.Add(construction);
            this.Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Box_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((TypeBox.SelectedIndex >= 0) && (StructureBox.SelectedIndex >= 0) &&
                (ConductorBox.SelectedIndex >= 0) && (SemiCondBox.SelectedIndex >= 0))
            {
                switch (TypeBox.SelectedIndex)
                {
                    case 0://Miner
                        InBox1.Visible = false;
                        InBox2.Visible = false;
                        OutBox1.Visible = true;
                        OutBox2.Visible = false;
                        OutBox1.Items.Clear();
                        foreach (Cargo stuff in here.composition)
                        {
                            OutBox1.Items.Add(stuff.name);
                        }
                        OutBox1.SelectedIndex = 0;
                        BuildButton.Enabled = true;
                        break;
                    case 1://Solar Plant
                        MessageBox.Show("Solar plants are not included yet.");
                        TypeBox.SelectedIndex = 0;
                        break;
                    case 2://Factory
                        MessageBox.Show("Factories are not included yet.");//InBox1 does materials, __Box2's will be objects in and out
                        TypeBox.SelectedIndex = 0;
                        break;
                    case 3://Molecular Foundry
                        InBox1.Visible = true;
                        InBox2.Visible = false;
                        OutBox1.Visible = true;
                        OutBox2.Visible = false;
                        InBox1.Items.Clear();
                        InBox1.Items.Add("Separate");
                        InBox1.Items.Add("Create");
                        OutBox1.Items.Clear();
                        foreach (SuperCargo stuff in Globals.materials.Values)
                        {
                            if (!stuff.elemental)
                            {
                                OutBox1.Items.Add(stuff.name);
                            }
                        }
                        BuildButton.Enabled = true;
                        break;
                    case 4://Atomic Foundry
                        MessageBox.Show("Atomic Foundries are not included yet.");
                        TypeBox.SelectedIndex = 0;
                        break;
                    case 5://Shipyard
                        MessageBox.Show("Shipyards are not included yet.");
                        TypeBox.SelectedIndex = 0;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
